package contacts.controller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import contacts.dao.impl.JTableDaoImpl;
import contacts.dao.impl.RoomDaoImpl;
import contacts.po.Room;
import contacts.po.Student;
import contacts.service.ContactsService;
import contacts.view.ContactsDialog;
import contacts.view.ContactsFrame;
import contacts.view.TreeWin;

public class ContactsControllerListener implements ActionListener, FocusListener {

	private TreeWin treeWin;
	private ContactsDialog contactsDialog;
	private ContactsFrame contactsFrame;
	private ContactsService contactsService;
	private JTableDaoImpl tableDao;

	public ContactsControllerListener() {
		contactsService = new ContactsService();
		tableDao = new JTableDaoImpl();
	}

	/**
	 * 事件处理 增删改查
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// 打开新增对话框
		if ("addStudentDialog".equals(e.getActionCommand())) {
			contactsDialog = new ContactsDialog(contactsFrame, "添加学生信息", null);
		}
		// 新增提交
		else if ("addPost".equals(e.getActionCommand())) {
			Student newStudent = this.getStudentInstance();
			if (newStudent != null) {
				contactsService.addStudent(newStudent);
				JOptionPane.showMessageDialog(contactsFrame, "添加学生" + tableDao.getMessage(newStudent, 2) + "的信息成功！");
				contactsDialog.dispose();
				contactsFrame.remove(treeWin);
				treeWin = new TreeWin(ContactsService.findAllStudent());
				contactsFrame.add(treeWin, BorderLayout.CENTER);
				contactsFrame.validate();
				JOptionPane.showMessageDialog(contactsFrame, "重新加载数据成功");
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "添加学生信息失败！");
			}
		}
		// 打开删除对话框
		else if ("deleteStudentDialog".equals(e.getActionCommand())) {
			int selRow = treeWin.getSelRow();
			if (selRow < 0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数据！");
			} else {
				Student student = ContactsService.findAllStudent().get(treeWin.getSelRow());
				contactsDialog = new ContactsDialog(contactsFrame, "删除学生信息", student);
			}
		}
		// 删除提交
		else if ("deletePost".equals(e.getActionCommand())) {
			String studentIdInput = contactsDialog.getStudentIdTextField().getText().trim();
			if (studentIdInput != null && !"".equals(studentIdInput)) {
				if (studentIdInput.length() == 12) {
					contactsService.deleteStudent(studentIdInput);
					JOptionPane.showMessageDialog(contactsFrame, "删除学号为" + studentIdInput + "的学生信息成功！");
					contactsDialog.dispose();
					contactsFrame.remove(treeWin);
					treeWin = new TreeWin(ContactsService.findAllStudent());
					contactsFrame.add(treeWin, BorderLayout.CENTER);
					contactsFrame.validate();
					JOptionPane.showMessageDialog(contactsFrame, "重新加载数据成功");
				} else {
					JOptionPane.showMessageDialog(contactsFrame, "请输入正确的学号！");
				}
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "请输入学号！");
			}
		}
		// 打开修改对话框
		else if ("updateStudentDialog".equals(e.getActionCommand())) {
			int selRow = treeWin.getSelRow();
			if (selRow < 0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数据！");
			} else {
				Student student = ContactsService.findAllStudent().get(treeWin.getSelRow());
				contactsDialog = new ContactsDialog(contactsFrame, "修改学生信息", student);
			}
		}
		// 修改提交
		else if ("updatePost".equals(e.getActionCommand())) {
			Student newStudent = this.getStudentInstance();
			if (newStudent != null) {
				contactsService.updateStudent(newStudent);
				JOptionPane.showMessageDialog(contactsFrame, "修改学生" + tableDao.getMessage(newStudent, 2) + "的信息成功！");
				contactsDialog.dispose();
				contactsFrame.remove(treeWin);
				treeWin = new TreeWin(ContactsService.findAllStudent());
				contactsFrame.add(treeWin, BorderLayout.CENTER);
				contactsFrame.validate();
				JOptionPane.showMessageDialog(contactsFrame, "重新加载数据成功");
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "修改学生信息失败！");
			}
		}
		// 通过查询框查询学生信息
		if ("searchPost".equals(e.getActionCommand())) {
			String studentIdInput = contactsFrame.getSearchtext().getText().trim();
			if (studentIdInput != null && !"".equals(studentIdInput)) {
				if (studentIdInput.length() == 12) {
					Student s = contactsService.getStudent(studentIdInput);
					if (s != null && !"".equals(studentIdInput)) {
					Object columnName[] = { "信息" };
					Object[][] data = new Object[7][1];
					for (int i = 0; i < 7; i++) {
						for (int j = 0; j < 1; j++) {
							data[i][j] = tableDao.getMessage(s, i + 1);
						}
					}
					JTable title = new JTable(data, columnName);
					title.setFont(new Font("", Font.BOLD, 20));
					title.setRowHeight(90);
					title.setEnabled(false);
					title.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
					title.setFillsViewportHeight(true);
					treeWin.remove(treeWin.getRightText());
					treeWin.messageJPanel = new JPanel(new BorderLayout());
					treeWin.tableTitle();
					treeWin.getMessageJPanel().add(title, BorderLayout.CENTER);
					treeWin.rightText = new JScrollPane(treeWin.messageJPanel);
					treeWin.add(treeWin.rightText, BorderLayout.CENTER);
					treeWin.validate();}
					else {
						JOptionPane.showMessageDialog(contactsFrame, "该学生不存在，请输入正确的学号！");
					}
				} else {
					JOptionPane.showMessageDialog(contactsFrame, "请输入正确的学号！");
				}
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "请输入学号！");
			}
		}
		// 查询所有学生信息
		if ("refreshStudentJTable".equals(e.getActionCommand())) {
			contactsFrame.remove(treeWin);
			treeWin = new TreeWin(ContactsService.findAllStudent());
			contactsFrame.add(treeWin, BorderLayout.CENTER);
			contactsFrame.validate();
			JOptionPane.showMessageDialog(contactsFrame, "查询成功");
		}

	}

	/**
	 * 焦点
	 */
	@Override
	public void focusGained(FocusEvent e) {
		String temp = contactsDialog.getBirthdayTextField().getText().trim();
		if (temp.equals("xxxx-xx-xx")) {
			contactsDialog.getBirthdayTextField().setText("");
			contactsDialog.getBirthdayTextField().setForeground(Color.BLACK);
		}
	}

	/**
	 * 焦点
	 */
	@Override
	public void focusLost(FocusEvent e) {
		String temp = contactsDialog.getBirthdayTextField().getText().trim();
		if (temp.equals("")) {
			contactsDialog.getBirthdayTextField().setForeground(Color.GRAY);
			contactsDialog.getBirthdayTextField().setText("xxxx-xx-xx");
		}
	}

	/**
	 * 获取学生vo实例 Student为dialog传递的vo对象
	 * 
	 * @return student
	 */
	public Student getStudentInstance() {
		Student student = null;
		String studentId = null;
		String studentName = null;
		String birthday = null;
		String phone = null;
		String email = null;
		String address = null;
		Room room = null;
		String roomNo = null;
		RoomDaoImpl roomDao = new RoomDaoImpl();
		int roomId = 0;
		// 学号输入框
		String studentIdInput = contactsDialog.getStudentIdTextField().getText().trim();
		if (studentIdInput != null && !"".equals(studentIdInput)) {
			if (studentIdInput.matches("[0-9]{0,12}") && studentIdInput.length() == 12) {
				studentId = studentIdInput;
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "请输入正确的学号", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "学号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		// 姓名输入框
		String studentNameInput = contactsDialog.getStudentNameTextField().getText().trim();
		if (studentNameInput != null && !"".equals(studentNameInput)) {
			studentName = studentNameInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "姓名不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		// 宿舍号输入框
		String roomNoInput = contactsDialog.getRoomNoTextField().getText().trim();
		if (roomNoInput != null && !"".equals(roomNoInput)) {
			if (roomNoInput.matches("[0-9]{0,3}") && roomNoInput.length() == 3) {
				if(roomNoInput.equals("121")||roomNoInput.equals("122")||roomNoInput.equals("123")||roomNoInput.equals("124")||roomNoInput.equals("420")||roomNoInput.equals("421")||roomNoInput.equals("422")||roomNoInput.equals("423")||roomNoInput.equals("424")) {
					roomNo = roomNoInput;
				}
				else {
					JOptionPane.showMessageDialog(contactsFrame, "该宿舍不存在，请输入正确的宿舍号", "输入错误", JOptionPane.ERROR_MESSAGE);
				}
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "请输入正确的宿舍号", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "宿舍号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		// 生日输入框
		String birthdayInput = contactsDialog.getBirthdayTextField().getText().trim();
		if (birthdayInput != null && !"".equals(birthdayInput)) {
			if (birthdayInput.matches("[0-9]{0,4}-[0-9]{0,2}-[0-9]{0,2}") && birthdayInput.length() == 10) {
				birthday = birthdayInput;
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "请以正确的格式输入生日", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "生日不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		// 电话输入框
		String phoneInput = contactsDialog.getPhoneTextField().getText().trim();
		if (phoneInput != null && !"".equals(phoneInput)) {
			if (phoneInput.matches("[0-9]{0,11}") && phoneInput.length() == 11) {
				phone = phoneInput;
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "请输入正确的电话", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "电话不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		// 邮箱输入框
		String emailInput = contactsDialog.getEmailTextField().getText().trim();
		if (emailInput != null && !"".equals(emailInput)) {
			email = emailInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "邮箱不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		// 地址输入框
		String addressInput = contactsDialog.getAddressTextField().getText().trim();
		if (addressInput != null && !"".equals(addressInput)) {
			address = addressInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "地址不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}

		room = new Room(roomNo);
		roomId = roomDao.getRoomId(room);
		if (studentId != null && studentName != null && birthday != null && phone != null && email != null
				&& address != null && roomNo != null) {
			student = new Student(studentId, studentName, birthday, phone, email, address, new Room(roomId, roomNo));
		}

		return student;
	}

	public void setContactsFrame(ContactsFrame contactsFrame) {
		this.contactsFrame = contactsFrame;
	}

	public ContactsService getContactsService() {
		return contactsService;
	}

	public void setStudentDialog(ContactsDialog contactsDialog) {
		this.contactsDialog = contactsDialog;
	}

	public void setTreeWin(JPanel treeWin) {
		this.treeWin = (TreeWin) treeWin;
	}

}
